#!bin/bash

base="{{cbase}}/supervisord"
key="{{cbase}}/supervisord/supervisord.conf"
ctl="{{cbase}}/miniconda3/bin/supervisord"

getpid() {
  pid=`ps aux | grep -v grep | grep $key | awk '{print $2}'`
  if [ -n "${pid}" ]; then
    return 0
  else
    return 1
  fi
}
pid=`ps aux | grep -v grep | grep $key | grep $ctl | awk '{print $2}'`
if [ -z "$pid" ]; then
  getpid
  if [ $? -eq 0 ]; then
    echo "killing:
    `ps aux | grep -v grep | grep $pid`"
    kill -9 $pid 
    sleep 1
    flag=0
    while [ $flag -eq 0 ]; do
      getpid
      flag=$?
    done
    echo "killed"
  fi
  nohup /bin/bash $base/boot.sh > $base/logs/boot.log 2>&1 &
else
  echo "service is running" > $base/logs/boot.log
fi

